<?php

namespace App\Http\Controllers\Api;

use App\Consts\GlobalCookieConst;
use App\Consts\GlobalLoginConst;
use App\Http\Controllers\BaseController;
use App\Models\Cache\Cache;
use App\Models\Cache\CacheKey;
use Illuminate\Http\Request;

class InterController extends BaseController
{
    public $validate = [
        "allow" => ["rules" => [
            "id" => "max:24",
        ]],
    ];

    /**
     * @path /inter/allow
     * @category 登录
     * @title 【有备注】检测登陆态
     * @method POST
     * @params [{"name":"id","type":"text","required":0,"example":"","desc":"游客ID"}]
     * @resBody {"success":true,"statusCode":200,"result":[{"id":"游客ID","identity":3,"nickname":"游客昵称","phone":"","status":1,"avatar":"","login_method":1,"server_time":1606658564,"access_token":"令牌"},{"id":"用户ID","phone":"手机号","nickname":"用户昵称","avatar":"用户头像，如1.jpg","status":"用户状态，0异常，1正常","special_signature":"个性签名","birthday":"生日时间戳","gender":"性别，1男，2女，3保密","description":"个人简介","province":"省ID","city":"市ID","industry":["1","2"],"job":[],"education":"学历ID","is_new":false,"created_at":"创建时间戳","access_token":"令牌","identity":1,"login_method":"登录方式，1手机号验证码，2手机号密码，3QQ，4微信，5微博","server_time":"服务器时间"}],"message":"OK"}
     * @description 1.实例返回中包含了【游客】【用户】两种节点
     */
    public function allow(Request $request)
    {
        $params = $request->all();
        $params[GlobalLoginConst::LOGIN_STATE_INFO_TOKEN] = $this->token;

        if (!$this->user) {
            $tourist = service()->InterService->getAllowTouristInfo($params);

            return $this->apiReturn($tourist);
        }

        $params[GlobalLoginConst::LOGIN_STATE_INFO_USER] = $this->user;
        $user = service()->InterService->getAllowUserInfo($params);
        if (!$user) {
            $tourist = service()->InterService->getAllowTouristInfo($params);

            return $this->apiReturn($tourist);
        }

        return $this->apiReturn($user);
    }

    /**
     * @path /inter/logout
     * @category 登录
     * @title 退出登录
     * @method POST
     * @resBody {"success":true,"statusCode":200,"result":true,"message":"OK"}
     */
    public function logout(Request $request)
    {
        // 从缓存中移除access_token
        Cache::getInstance([
            'platform' => GlobalLoginConst::LOGIN_PLATFORM_WEB_STR,
            'token' => $this->token,
        ])->delete(CacheKey::USER_LOGIN_STATUS);

        return $this->apiReturn(true)->cookie(GlobalCookieConst::TOKEN, '');
    }

}
